<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%@page import="helper.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/clientTemplate.css">
        <link rel="stylesheet" type="text/css" href="css/mailControl.css">
        <script type="text/javascript">
            function redirect(page)
            {
                window.location = page;
            }
        </script>
        <title>Client Template</title>
    </head>
    <body>
        <center>
            <table id="tableClientTemplate">
                <tr id="headerClientTemplate">
                    <td><%@include file="clientHeader.jsp" %></td>
                </tr>
                <tr id="middleClientTemplate">
                    <td>

                        <table>
                            <td class="tdControl2"><a class="three" href="inbox.jsp">Inbox</a></td>
                            <td class="tdControl2"><a class="three" href="sentMail.jsp">Sent Mail</a></td>
                            <td class="tdControl2"><a class="three" href="composeMail.jsp">Compose Mail</a></td>
                        </table>
                        <br/>
                        <%
                            int mailID = Integer.parseInt(request.getParameter("mailID"));
                            String fromUser = "";
                            String toUser = "";
                            String cc = "";
                            String bcc = "";
                            String subject = "";
                            String sentDate = "";
                            String mailContent = "";
                            Boolean hasFile = false;
                            String userName = (String)request.getSession().getAttribute("username");
                            
                            try {

                                Connection con = helper.ConnectionHelper.GetConnection();
                                String sql = "SELECT FromUser, ToUser, Cc, Bcc, Subject, "
                                        + "SentDate, MailContent, HasFile FROM Mails WHERE MailID = ?";
                                PreparedStatement p = con.prepareStatement(sql);
                                p.setInt(1, mailID);
                                ResultSet rs = p.executeQuery();

                                if (rs.next())
                                {
                                    fromUser = rs.getString("FromUser");
                                    toUser = rs.getString("ToUser");
                                    cc = rs.getString("Cc");
                                    bcc = rs.getString("Bcc");
                                    subject = rs.getString("Subject");
                                    sentDate = rs.getString("SentDate");
                                    mailContent = rs.getString("MailContent");
                                    hasFile = rs.getBoolean("HasFile");
                                }

                            } 
                            catch (SQLException e)
                            {
                                System.out.println("SQL Exception: " + e.toString());
                            }
                        %>
                        <table border="1" style="text-align: left">
                            <tr>
                                <td style="width: 80px">From</td>
                                <td style="width: 900px"><% out.print(fromUser); %></td>
                            </tr>
                            <tr>
                                <td>To</td>
                                <td><% out.print(toUser); %></td>
                            </tr>
                            <%
                                if(cc != null && !cc.equals(""))
                                {
                                    out.print("<tr><td>Cc</td><td>" + cc + "</td></tr>");
                                }
                            %>
                            <%
                                if(bcc != null && !bcc.equals(""))
                                {
                                    out.print("<tr><td>Bcc</td><td>" + bcc + "</td></tr>");
                                }
                            %>
                            <tr>
                                <td>Subject</td>
                                <td><% out.print(subject); %></td>
                            </tr>
                            <tr>
                                <td>Date</td>
                                <td><% out.print(sentDate); %></td>
                            </tr>
                            <tr>
                                <td colspan="2" style="background-color: white">
                                    <% out.print(mailContent); %>
                                </td>
                            </tr>
                            <%
                                if(hasFile)
                                {
                                    if(fromUser.indexOf(userName) < 0)
                                    {
                                        String headerFromUser = fromUser.substring(0, fromUser.indexOf("@"));
                                        int referenceMailID = 0;
                                        try
                                        {
                                            Connection con = helper.ConnectionHelper.GetConnection();
                                            String sql = "SELECT MailID FROM Mails WHERE UserName = ? AND SentDate = ?";
                                            PreparedStatement p = con.prepareStatement(sql);
                                            p.setString(1, headerFromUser);
                                            p.setString(2, sentDate);
                                            ResultSet rs = p.executeQuery();

                                            if (rs.next())
                                            {
                                                referenceMailID = rs.getInt("MailID");
                                            }
                                        }
                                        catch (SQLException e)
                                        {
                                            System.out.println("SQL Exception: " + e.toString());
                                        }

                                        mailID = referenceMailID;
                                    }

                                    String path = request.getServletContext().getRealPath("/files/" + mailID);
                                    File dir = new File(path);
                                    String[] children = dir.list();
                                    if (children == null)
                                    {
                                        // Either dir does not exist or is not a directory
                                    }
                                    else
                                    {
                                        for (int i=0; i<children.length; i++)
                                        {
                                            // Get filename of file or directory
                                            String filename = children[i];
                                            out.print("<tr><td colspan='2'>" + filename + 
                                                    "&nbsp;-&nbsp;<a href='files/" + mailID + "/" + filename +
                                                    "'>Download</a></td></tr>");
                                        }
                                    }
                                }
                            %>
                            <tr>
                                <td colspan="2" align="center">
                                    <%
                                        String user = fromUser;
                                        if(fromUser.indexOf(userName) >= 0)
                                        {
                                            user = toUser;
                                        }
                                    %>
                                    <input type="button" onclick="redirect('composeMail.jsp?reply=<% out.print(user); %>')"
                                           value="Reply">
                                </td>
                            </tr>
                        </table>

                    </td>
                </tr>
                <tr id="footerClientTemplate">
                    <td><%@include file="clientFooter.jsp" %></td>
                </tr>
            </table>
        </center>
    </body>
</html>
